package com.lagou.homework.second;

import java.util.Scanner;

public class FiveInRow {

    /**
     * 行动方
     */
    private static int type;

    /**
     * 棋盘数组
     */
    private static char[][] arr;
    /**
     * 下子次数
     */
    private static int cnt;

    /**
     * 落子点横坐标
     */
    private static int x;

    /**
     * 落子点纵坐标
     */
    private static int y;

    /**
     * 绘制棋盘
     */
    private static void board() {
        if (0 == cnt) {
            //初始化棋盘
            for(int i = 0; i < arr.length; i++) {
                for(int j = 0; j < arr[i].length; j++) {
                    if(0 == i) {
                        int num = j - 1;
                        if(num <= 9) {
                            if(0 == j) {
                                arr[i][j] = ' ';
                            } else {
                                arr[i][j] = (char)(num + 48);
                            }
                        } else {
                            arr[i][j] = (char)(num + 87);
                        }
                    } else {
                        int num = i - 1;
                        if(0 != j) {
                            arr[i][j] = '+';
                        } else {
                            if(num > 9) {
                                arr[i][j] = (char)(num + 87);
                            } else {
                                arr[i][j] = (char)(num + 48);
                            }
                        }
                    }
                }
            }
        } else {
            if (type == 1) {
                arr[x][y] = '#'; //黑
            } else if (type == 2) {
                arr[x][y] = '@'; //白
            }
        }

        //绘制棋盘
        for(int i = 0; i < arr.length; i++) {
            for(int j = 0; j < arr[i].length; j++) {
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
    }

    /**
     * 提示黑方和白方分别下棋并重新绘制棋盘
     */
    private static void play(Scanner sc) {
        String role;
        if (type == 1) {
            role = "黑";
        } else {
            role = "白";
        }
        System.out.println("该" + role + "方下棋了：");

        boolean b;
        do {
            x = sc.nextInt() + 1;
            y = sc.nextInt() + 1;

            if (0 == x || 0 == y || x > 16 || y > 16) {
                System.out.println("该位置超出棋盘范围，请重新下棋");
                b = true;
            } else {
                if (arr[x][y] != '+') {
                    System.out.println("该位置已有棋子，请重新下棋");
                    b = true;
                } else {
                    cnt++;
                    board();
                    b = false;
                }
            }
        } while (b);
    }

    /**
     * 判断是否获胜
     */
    private static boolean check() {
        //前8次不需要判断是否获胜
        if (cnt < 9) {
            return false;
        }

        char sign = '#';
        if (type == 2) {
            sign = '@';
        }

        //判断当前行动方是否有五子连棋
        //上到下判断是否有五子连棋
        int num = 1;
        int i = x;
        int j = y;

        //向下查找
        while (sign == arr[++i][j] && i < 16) {
            num++;
            if (num >= 5) {
                return true;
            }
        }

        //向上查找
        if (num < 5) {
            //回到落子点
            i = x;
            j = y;
            while (sign == arr[--i][j] && i > 0) {
                num++;
                if (num >= 5) {
                    return true;
                }
            }
        }

        //左到右判断是否有五子连棋
        num = 1;
        i = x;
        j = y;

        //向右查找
        while (sign == arr[i][++j] && j < 16) {
            num++;
            if (num >= 5) {
                return true;
            }
        }

        //向左查找
        if (num < 5) {
            //回到落子点
            i = x;
            j = y;
            while (sign == arr[i][--j] && j > 0) {
                num++;
                if (num >= 5) {
                    return true;
                }
            }
        }

        //左上到右下判断是否有五子连棋
        num = 1;
        i = x;
        j = y;

        //向右下查找
        while (sign == arr[++i][++j] && j < 16 && i < 16) {
            num++;
            if (num >= 5) {
                return true;
            }
        }

        //向左上查找
        if (num < 5) {
            //回到落子点
            i = x;
            j = y;
            while (sign == arr[--i][--j] && j > 0 && i > 0) {
                num++;
                if (num >= 5) {
                    return true;
                }
            }
        }

        //左下到右上判断是否有五子连棋
        num = 1;
        i = x;
        j = y;

        //向右上查找
        while (sign == arr[--i][++j] && j < 16 && i > 0) {
            num++;
            if (num >= 5) {
                return true;
            }
        }

        //向左下查找
        if (num < 5) {
            //回到落子点
            i = x;
            j = y;
            while (sign == arr[++i][--j] && j > 0 && i < 16 ) {
                num++;
                if (num >= 5) {
                    return true;
                }
            }
        }

        return false;
    }

    /**
     * 切换玩家行动
     */
    private static void changePlayer() {
        //如果无获胜记录切换下次下棋方
        type = type == 1 ? 2 : 1;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        arr = new char[17][17];
        //初始化棋盘
        board();

        System.out.println("黑方：#;白方：@");
        System.out.println("请选择先手（黑：1、白：2）：");

        type = sc.nextInt();

        if (type == 1 || type == 2) {
            boolean flag = true;
            while (flag) {
                play(sc);
                //如果没有五子连棋切换玩家
                if (!check()) {
                    changePlayer();
                } else {
                    //打印获胜方
                    String player = type == 1 ? "黑方" : "白方";
                    System.out.println("游戏结束，获胜方为：" + player + "!");
                    flag = false;
                }
            }
        } else {
            System.out.println("选择错误!退出程序");
        }
    }
}
